<!DOCTYPE html>
<html>
<head>
    <title>游记列表</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travel.css"/>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/plugins/dialog/dialog.min.js"></script>

    <script>
        $(function () {

        });

    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜游记">
            </div>
        </div>
    </div>
</div>


<div class="container">
    <h6 class="module-title text-center">每月冠军游记</h6>
    <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
        <div id="travelsCommends" class="carousel-inner" render-loop="list">
            <div class="carousel-item">
                <div class="border commend">
                    <a href="" render-key="list.travelId" render-fun="handle">
                        <img width="100%" height="200px" render-src="list.coverUrl">
                        <h1 render-html="list.title"></h1>
                        <small render-html="list.placeName"></small>
                    </a>
                </div>
            </div>
        </div>

        <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only"></span>
        </a>
        <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only"></span>
        </a>
    </div>
</div>

<div class="container travels">
    <h6>全部游记</h6>
    <div id="allTravels" render-loop="list">

        <div class="row detail">
            <div class="col">
                <a href="travelContent.html" render-key="list.id" render-fun="handle">
                    <h4 render-html="list.title"></h4>
                </a>
                <p>
                    <a href="userProfiles.html" render-key="list.author.id" render-fun="getUser">
                        <img class="rounded-circle head-img" render-src="list.author.headImgUrl">
                        <span render-html="list.author.nickName"></span> </a> 在
                    <span class="addr" render-html="list.place.name">成都</span>
                </p>
                <p>
                    <i class="fa fa-eye fa-fw"></i>
                    <span>100</span>
                    <i class="fa fa-commenting fa-fw"></i>
                    <span render-key="list.id" render-fun="manage">100</span>
                </p>
            </div>
            <div class="col bigimg">
                <a href="travelContent.html" render-key="list.id" render-fun="handle">
                    <img render-src="list.coverUrl">
                </a>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    // 定义全局变量当前页
    var currentPage = 1;
    // 定义总页数
    var pages;
    // 定义全局变量游记数组
    var travels;
    // 页面加载完毕执行
    $(function () {

        // 月推荐游记功能
        // 页面加载完毕发送请求获取月推荐游记的数据
        $.get("/travels/commends", {type: 2}, function (data) {
            // 渲染页面数据
            $("#carouselExampleControls").renderValues({list: data}, {
                // 动态渲染 a 标签
                handle: function (ele, value) {
                    ele.href = "/travelContent.html?id=" + value;

                }
            });
            // 显示第一个推荐, 给第一个推荐游记设置class属性: active
            $("#travelsCommends>div:first").addClass("active");

        }, "json");


        // 发送ajax请求获取游记数据
        $.get("/travels", {"isPublic": true, "state": 2, "currentPage": 1, "pageSize": 4}, function (data) {
            console.log(data);
            // 保存游记数据
            travels = data.list;
            // 保存总也数
            pages = data.pages;
            // 渲染页面
            renderViews(data);
        });
        // 分页效果
        // 定义一个监听屏幕滑动事件
        $(window).scroll(function () {
            // 判断是否触底
            if (isEnd()) {
                // 判断是否还有下一页
                if (currentPage < pages) {
                    // 当前页+1
                    currentPage++;
                    // 发送ajax请求
                    var args = {isPublic: true, state: 2, currentPage: currentPage, pageSize: 4};
                    $.get("/travels", args, function (data) {
                        // 拼接游记数组数据
                        $.merge(travels, data.list);
                        // 渲染页面
                        renderViews({list: travels});
                    });
                } else {
                    // 否则, 提示已到低
                    $(document).dialog({
                        type: 'notice',
                        infoText: '已到底部',
                        autoClose: 2500,
                        position: 'bottom'  // center: 居中; bottom: 底部
                    });
                }
            }
        });

        // 定义一个判断屏幕溢出高度 + 屏幕高度 > 网页高度的函数
        function isEnd() {
            return $(window).height() + $(window).scrollTop() >= $(document).height();
        }

        // 渲染页面的方法
        function renderViews(data) {
            $(".travels").renderValues(data, {
                // 动态渲染a标签
                handle: function (ele, value) {
                    $(ele).attr("href", "/travelContent.html?id=" + value);
                    //渲染跳转到个人主页的a标签
                },
                getUser: function (ele, value) {
                    console.log(value);
                    $(ele).attr("href", "/userProfiles.html?authorId=" + value);
                },
                manage:function (ele, value) {
                    //将异步请求变成同步请求,可以回显点评论数
                    $.ajax({
                        url: "/travels/"+value+"/replyCount",
                        type: "get",
                        async: false,
                        success: function (data) {
                            $(ele).html(data.replyCount)
                        }
                    })
                }
            });
        }
    });

</script>

</html>